/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.talleres.Bl.TallerBlLocal;
import mx.its.talleres.modelo.Taller;

/**
 *
 * @author Yessy
 */
@ManagedBean
@SessionScoped
public class TallerBean {
    @EJB
    private TallerBlLocal tallerBl;

    /**
     * Creates a new instance of TallerBean
     */
     private List<Taller> lista;
    private Taller taller;
    private String accion;
    public TallerBean() {
    }

    public Taller getTaller() {
        return taller;
    }

    public void setTaller(Taller taller) {
        this.taller = taller;
    }

    public List<Taller> getLista() {
        lista= tallerBl.getAll();
        return lista;
    }
     public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        tallerBl.registrar(taller);
                }else{
        if(accion.equalsIgnoreCase("Editar")){
        tallerBl.editar(taller);
        }
        else{
        if(accion.equalsIgnoreCase("Eliminar")) {
                tallerBl.eliminar(taller);
            }
        }
        }

        return "TallerLista";
    }

    //Listeners
    public void prepararNuevoRegistro(ActionEvent evt) {
        accion="Nuevo";
        taller = new Taller();
    }
   public void prepararEditar(ActionEvent evt) {
       accion="Editar";
      System.out.println(evt.getComponent().getClientId());        
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        taller=lista.get(index);
    } 
   public void prepararEliminar(ActionEvent evt){
   accion="Eliminar";
   System.out.println(evt.getComponent().getClientId());
    int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        taller=lista.get(index);
   
   }
}
